package cn.yudao.pet.controller;

import cn.yudao.basic.exception.BusinessException;
import cn.yudao.basic.utils.LoginContext;
import cn.yudao.org.domain.Shop;
import cn.yudao.org.service.IShopService;
import cn.yudao.pet.dto.AcceptDto;
import cn.yudao.pet.service.ISearchMasterMsgService;
import cn.yudao.pet.domain.SearchMasterMsg;
import cn.yudao.pet.query.SearchMasterMsgQuery;
import cn.yudao.basic.utils.AjaxResult;
import cn.yudao.basic.utils.PageList;
import cn.yudao.user.domain.Logininfo;
import cn.yudao.user.domain.User;
import cn.yudao.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/searchMasterMsg")
public class SearchMasterMsgController {
    @Autowired
    public ISearchMasterMsgService searchMasterMsgService;

    @Autowired
    private IUserService userService;


    @Autowired
    private IShopService shopService;



    /**
     * 保存和修改公用的
     * @param searchMasterMsg  传递的实体
     * @return Ajaxresult转换结果
     */
    @PutMapping
    public AjaxResult addOrupadte(@RequestBody SearchMasterMsg searchMasterMsg){
        try {
            if( searchMasterMsg.getId()!=null)
                searchMasterMsgService.upadte(searchMasterMsg);
            else
                searchMasterMsgService.save(searchMasterMsg);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }
    /**
    * 删除对象信息
    * @param id
    * @return
    */
    @DeleteMapping(value="/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.removeById(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }
	
    //获取用户
    @GetMapping("/{id}")
    public SearchMasterMsg get(@PathVariable("id")Long id)
    {
        return searchMasterMsgService.LoadById(id);
    }


    /**
    * 查看所有的员工信息
    * @return
    */
    @GetMapping()
    public List<SearchMasterMsg> list(){

        return searchMasterMsgService.loadAll();
    }


    /**
    * 分页查询数据
    *
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping("/list")
    public PageList<SearchMasterMsg> json(@RequestBody SearchMasterMsgQuery query)
    {
        return searchMasterMsgService.query(query);
    }

    // 发布信息
    @PostMapping("/publish")
    public AjaxResult publish(@RequestBody SearchMasterMsg searchMasterMsg, HttpServletRequest request){
        try {
            // 获取到用户
            Logininfo logininfo = LoginContext.getLogininfo(request);
            // 根据logininfo的id查询到user
            User user = userService.loadByLogininfoId(logininfo.getId());
            // 设置searchMasterMsg的userid字段值
            searchMasterMsg.setUserId(user.getId());
            searchMasterMsgService.publish(searchMasterMsg);
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

    /**
    * @Title: toHandle
    * @Description: 待处理 需要店铺id以及state=1
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/20 12:45
    * @Parameters:
    * @Return
    */
    @PostMapping("/toHandle")
    public PageList<SearchMasterMsg> ToHandle(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request)
    {
        // 设置状态为待处理 审核通过
        query.setState(1);
        // 设置当前店铺的id值
        Logininfo logininfo = LoginContext.getLogininfo(request);
        // 得到对应的shop
       Shop shop =  shopService.loadBylogininfoId(logininfo.getId());
       // 将shopid的值得到赋值
        query.setShopId(shop.getId());
        System.out.println("进来了");
        return searchMasterMsgService.query(query);
    }

    /**
    * @Title: ToAudit
    * @Description: 待审核信息
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/20 12:59
    * @Parameters:
    * @Return
    */
    @PostMapping("/toAudit")
    public PageList<SearchMasterMsg> ToAudit(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request)
    {
        // 设置状态为待审核
        query.setState(0);
        return searchMasterMsgService.query(query);
    }

    /**
    * @Title: ToAudit
    * @Description: 寻主池
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/20 13:00
    * @Parameters:
    * @Return
    */
    @PostMapping("/toSearchMasterPool")
    public PageList<SearchMasterMsg> Pool(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request)
    {
        // 设置状态为审核通过的 并且shopid为null
        query.setState(1);
        return searchMasterMsgService.query(query);
    }

    /**
    * @Title: Pool
    * @Description: 已完成
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/20 13:01
    * @Parameters:
    * @Return
    */
    @PostMapping("/finish")
    public PageList<SearchMasterMsg> Finish(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request)
    {
        // 设置状态已完成 并且shopid为null
        query.setState(2);
        return searchMasterMsgService.query(query);
    }

    /**
    * @Title: Finish
    * @Description: 前台用户查看自己的寻主信息
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/20 13:02
    * @Parameters:
    * @Return
    */
    @PostMapping("/user")
    public PageList<SearchMasterMsg> User(@RequestBody SearchMasterMsgQuery query,HttpServletRequest request)
    {
        // 设置当前用户的id值
        Logininfo logininfo = LoginContext.getLogininfo(request);
        // 根据logininfo的id查询到user
        User user = userService.loadByLogininfoId(logininfo.getId());
        query.setUserId(user.getId());
        return searchMasterMsgService.query(query);
    }



    /**
    * @Title: publish
    * @Description: 确认接单
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/20 14:03
    * @Parameters:
    * @Return
    */
    @PostMapping("/accept")
    public AjaxResult acceptOrder(@RequestBody AcceptDto acceptDto){
        try {
            searchMasterMsgService.acceptOrder(acceptDto);
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }



    /**
    * @Title: reject
    * @Description: 拒绝订单业务层
    * @Author: Mr.Jia
    * @Version: 1.0
    * @Date:  2022/7/20 17:01
    * @Parameters:
    * @Return
    */
    @PostMapping("/reject/{id}")
    public AjaxResult reject(@PathVariable("id") Long id){
        try {
            searchMasterMsgService.reject(id);
            return AjaxResult.me();
        }catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage(e.getMessage());
        }catch (Exception e){
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙");
        }
    }

}
